package com.light.springboot.controller;

import com.light.springboot.base.jpa.repostitory.utils.DynamicSpecifications;
import com.light.springboot.base.jpa.repostitory.utils.OP;
import com.light.springboot.base.jpa.repostitory.utils.SearchFilter;
import com.light.springboot.model.Person;
import com.light.springboot.model.Person_;
import com.light.springboot.repository.PersonRepository;
import com.light.springboot.service.PersonService;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@RestController
public class DataController {
    @Resource
    PersonRepository personRepository;

    @Resource
    PersonService personService;

    /*@RequestMapping("/save")
    public Person save(String name,String address,Integer age) {
        name = "1";
        address = "2";
        age = 3;
        Person person = personRepository.save(new Person(null, name, age, address));
        return person;
    }

    @RequestMapping("/q1")
    public List<Person> q1(String address) {
        String sql = "select * from person where id = ?";
        List<Person> persons = personRepository.sqlQuery(sql, "1");
        return persons;
    }

    @RequestMapping("/q2")
    public Person q2(String address) {
        Person person = personRepository.findOne(1L);
        person.setAddress("abc");
        Person person1 = personRepository.update(person);
        return person1;
    }*/

    @RequestMapping("/q3")
    public List<Person> q3(String address) {
        Collection<SearchFilter> filters = Arrays.asList(
                new SearchFilter(Person_.id, OP.EQ, 1)
        );
        Specification<Person> objectSpecification = DynamicSpecifications.bySearchFilter(filters);
        List<Person> persons = personService.findAll(objectSpecification);
        return persons;
    }

    /*@RequestMapping("/q3")
    public Person q3(String name, String address) {
        Person person = personRepository.withNameAndAddressQuery(name, address);
        return person;
    }

    @RequestMapping("/q4")
    public Person q4(String name, String address) {
        Person person = personRepository.withNameAndAddressNamedQuery(name, address);
        return person;
    }
    @RequestMapping("/sort")
    public List<Person> sort() {
        List<Person> people = personRepository.findAll(new Sort(Sort.Direction.ASC, "age"));
        return people;
    }
    @RequestMapping("/page")
    public Page<Person> page(int page,int size){
        Page<Person> all = personRepository.findAll(new PageRequest(page, size));
        return all;
    }
    @RequestMapping("/all")
    public List<Person> all(){
        return personRepository.findAll();
    }*/
}